package com.lujianfei.excel2json;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import com.lujianfei.excel2json.mvp.MainContract;
import com.lujianfei.excel2json.mvp.PresenterImpl;
import com.lujianfei.lib.dialog.MessageBox;
import com.lujianfei.lib.sdautolayout.impl.SDAutoLayout;
import com.lujianfei.lib.utils.TextUtils;
import com.lujianfei.lib.widgets.JLabelEditBrowser;
import com.lujianfei.lib.widgets.JTextAreaEx;
import com.lujianfei.lib.window.BaseWindow;

public class MainWindow extends BaseWindow implements MainContract.View,ActionListener{

	MainContract.Presenter presenter;
	
	JTextAreaEx txtJsonOutput;
	
	JLabelEditBrowser mExcelPathBrower;
	
	JButton btStart;
	
	public MainWindow() {
		super(800,500);
		setTitle("Excel To Json");
		presenter = new PresenterImpl(this);
		
		initView();
		initEvent();
	}

	private void initEvent() {
		btStart.addActionListener(this);
	}

	private void initView() {
		btStart = new JButton("开始转换");
		mExcelPathBrower = new JLabelEditBrowser("Excel 文件:");
		JScrollPane jScrollPane = new JScrollPane(txtJsonOutput = new JTextAreaEx());
		add(jScrollPane);
		add(mExcelPathBrower);
		add(btStart);
		SDAutoLayout.forComponent(mExcelPathBrower).widthIs(getWidth()-120).heightIs(35).leftSpaceToComponent(this, 5);
		SDAutoLayout.forComponent(btStart).widthIs(90).heightIs(35).rightSpaceToComponent(this, 15);
		SDAutoLayout.forComponent(jScrollPane).widthIs(getWidth()-5).heightIs(getHeight()-30-mExcelPathBrower.getHeight()).topSpaceToComponent(mExcelPathBrower, 5);
		SDAutoLayout.forComponent(txtJsonOutput).widthIs(jScrollPane.getWidth()).heightIs(jScrollPane.getHeight());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(TextUtils.isEmpty(mExcelPathBrower.getText())) {
			MessageBox.show(this, "路径不能为空");
			return ;
		}
		File file = new File(mExcelPathBrower.getText());
		if(!file.exists()) {
			MessageBox.show(this, "文件不存在");
			return;
		}
		if(!file.getName().endsWith(".xls")) {
			MessageBox.show(this, "文件必须是.xls后缀");
			return;
		}
		presenter.excel2json(file);
	}
	

	@Override
	public void setOutputJson(String json) {
		// TODO Auto-generated method stub
		txtJsonOutput.setText(json);
	}
}
